package com.mall507.fivepeaks.activity.mine;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hzh.frame.ui.fragment.AbsRecyclerViewFM;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.MineMessage;
import com.mall507.comn.model.User;
import com.mall507.comn.tools.UserTools;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @date 2019/8/26
 */
@Route(path = "/mine/MessageRFM")
public class MessageCommentRFM extends AbsRecyclerViewFM<MineMessage>{
    String img = null;
    public static MessageCommentRFM newInstance(int type) {
        MessageCommentRFM myFragment = new MessageCommentRFM();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        myFragment.setArguments(bundle);
        return myFragment;
    }

    @Override
    public boolean setTitleIsShow() {
        return false;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fm_rv_mine_message_index;
    }

    @Override
    protected void bindView(View view) {
        
    }
    
    @Override
    protected String setHttpPath() {
        return "Me/getMessage";
    }

    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("type", getArguments().getInt("type"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected List<MineMessage> handleHttpData(JSONObject response) {
        List<MineMessage> list = new ArrayList<>();
        if(200==response.optInt("code")){
            JSONArray jsonArray=response.optJSONArray("data");
            for(int i=0;i<jsonArray.length();i++){
                JSONObject object=jsonArray.optJSONObject(i);
                MineMessage model=new MineMessage();
                model.setNid(object.optString("id"));
                model.setTitle(object.optString("title"));
                model.setCreate_at(object.optString("create_at"));
                model.setTime(object.optString("time"));
                model.setViews(object.optString("views"));
                model.setNickname(object.optString("nickname"));
                model.setP_content(object.optString("p_content"));
                model.setHead_img(object.optString("head_img"));
                JSONArray imgs = object.optJSONArray("imgs");
                String imgsize = String.valueOf(imgs.length());
                model.setImgSize(imgsize);
                if (imgs != null && imgs.length()>0){
                    img = String.valueOf(imgs.length());
                    model.setImgSize(img);
                    for (int j = 0;j<imgs.length();j++){
                        switch (j){
                            case 0:model.setIcon1(imgs.optString(j));break;
                            case 1:model.setIcon2(imgs.optString(j));break;
                            case 2:model.setIcon3(imgs.optString(j));break;
                        }
                    }
                }else {
                    img = "0";
                    model.setImgSize(img);
                    model.setIcon1("");
                    model.setIcon2("");
                    model.setIcon3("");
                }
                list.add(model);
            }
        }
        return list;
    }

    @Override
    protected void showLodingFail() {
        super.showLodingFail();
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_mine_message;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, MineMessage model) {
        ((CircleImageView) holder.getView(R.id.user_avatar)).setImageURI(model.getHead_img());
        holder.setText(R.id.user_name, model.getNickname()); //评论人头像
        holder.setText(R.id.system_message,model.getP_content());
        holder.setText(R.id.message_times,model.getCreate_at());
        holder.setText(R.id.comment,model.getTitle());
        if (img != null){
            if (model.getImgSize().equals("1")){
                ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
                ((ExpandImageView) holder.getView(R.id.image2)).setVisibility(View.INVISIBLE);
                ((ExpandImageView) holder.getView(R.id.image3)).setVisibility(View.INVISIBLE);
            }else if (model.getImgSize().equals("2")){
                ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
                ((ExpandImageView) holder.getView(R.id.image2)).setImageURI(model.getIcon2());
                ((ExpandImageView) holder.getView(R.id.image3)).setVisibility(View.INVISIBLE);
            }else if (model.getImgSize().equals("3")){
                ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
                ((ExpandImageView) holder.getView(R.id.image2)).setImageURI(model.getIcon2());
                ((ExpandImageView) holder.getView(R.id.image3)).setImageURI(model.getIcon3());
            }else if (model.getImgSize().equals("0")){
                ((ExpandImageView) holder.getView(R.id.image1)).setVisibility(View.GONE);
                ((ExpandImageView) holder.getView(R.id.image2)).setVisibility(View.GONE);
                ((ExpandImageView) holder.getView(R.id.image3)).setVisibility(View.GONE);
            }
        }
        holder.setText(R.id.browseNumber,model.getViews() + "人浏览");
        if (UserTools.getInstance().getIsLogin()) {
            User user = UserTools.getInstance().getUser();
            ((CircleImageView) holder.getView(R.id.img_avatar)).setImageURI(user.getHead());
            holder.setText(R.id.nice,user.getNickName() + "  " +  model.getTime());
        } else {
            User user = UserTools.getInstance().getUser();
            ((CircleImageView) holder.getView(R.id.img_avatar)).setImageURI(user.getHead());
            holder.setText(R.id.nice,user.getNickName() + "  " + model.getTime());
        }
    }
    
}
